home *** CD-ROM | disk | FTP | other *** search
/ PC User 2003 July / Disc 2 / PCU0703CD2.iso / entertn / demos / files / ron.exe / ai / scripts / aibestbuildlibrary.bhs next >
Encoding:
Text File  |  2003-04-29  |  26.1 KB  |  648 lines

  1. // best build library
  2.  
  3. //=================================================================
  4.  
  5. //city placement
  6. int ai city_placement (int who) 
  7. {
  8.  
  9.   new_city = 0;
  10.   
  11.  
  12.   enable_trigger("city_build");
  13.  
  14.   trigger city_build() {
  15.     if (have_tech(who, "City State") > 0) {
  16.       place_city_with_cost(who);
  17.       enable_trigger("health_check");
  18.     }
  19.     else {
  20.       research_tech_with_cost(who, "City State");
  21.       enable_trigger("city_build");
  22.     }
  23.   }
  24.  
  25.   trigger health_check() {
  26.     if (find_inactive_build(who, "Small City")) {
  27.       new_city = find_inactive_build(who, "Small City");
  28.       if (building_started(who, new_city)) {
  29.         return 1;
  30.       }
  31.       else  {
  32.         enable_trigger("health_check");
  33.       }
  34.     }
  35.     else {
  36.       enable_trigger("city_build");
  37.     }
  38.   }
  39.  
  40.   return -1;
  41.  
  42. }
  43.  
  44. //=================================================================
  45.  
  46. //woodcutter placement
  47. int ai place_woodcutter (int who)
  48. {
  49.  
  50.   int wood_camp_2 = 0;
  51.   int wood_camp_size_2 = 0;
  52.   my_capital = find_city_with_num(who, 1); 
  53.   my_second_city = find_city_with_num(who, 2);
  54.   static int min_size = 5;
  55.   int city_down = 1;
  56.   int num_check = 3;
  57.  
  58.   //placing woodcutter at capital, no other spots available
  59.   if ((num_cities(who) == 1) && (find_inactive_build(who, "Small City") < 0)) {
  60.     if (place_building_with_cost(who, "Woodcutter's Camp", my_capital) > 0) { //try capital
  61.       wood_camp_2 = find_inactive_build(who, "Woodcutter's Camp");
  62.       wood_camp_size_2 = max_workers_at_building(who, wood_camp_2);
  63.       if ((wood_camp_size_2 < min_size) && (min_size > 3)) {
  64.         destroy_building(who, wood_camp_2);
  65.         min_size--;
  66.       }
  67.       else {
  68.         min_size = 5;
  69.         return wood_camp_size_2;
  70.       }
  71.     }
  72.     else if (can_pay_cost(who, "Woodcutter's Camp")) return 0;
  73.   }
  74.   //placing woodcutter at second city that is still being built
  75.   else if (find_inactive_build(who, "Small City") > 0) {
  76.     my_new_city = find_inactive_build(who, "Small City");
  77.     if (place_orphan_building_with_cost(who, "Woodcutter's Camp", my_new_city) > 0) { //try near city under construction
  78.       wood_camp_2 = find_inactive_build(who, "Woodcutter's Camp");
  79.       wood_camp_size_2 = max_workers_at_building(who, wood_camp_2);
  80.       if ((wood_camp_size_2 < min_size) && (min_size > 3)) {
  81.         destroy_building(who, wood_camp_2);
  82.         min_size--;
  83.       }
  84.       else {
  85.         min_size = 5;
  86.         return wood_camp_size_2;
  87.       }
  88.     }
  89.     else if (place_building_with_cost(who, "Woodcutter's Camp", my_capital) > 0) { //try capital
  90.       wood_camp_2 = find_inactive_build(who, "Woodcutter's Camp");
  91.       wood_camp_size_2 = max_workers_at_building(who, wood_camp_2);
  92.       if ((wood_camp_size_2 < min_size) && (min_size > 3)) {
  93.         destroy_building(who, wood_camp_2);
  94.         min_size--;
  95.       }
  96.       else {
  97.         min_size = 5;
  98.         return wood_camp_size_2;
  99.       }
  100.     }
  101.     else if (can_pay_cost(who, "Woodcutter's Camp")) return 0;
  102.   }
  103.   //placing woodcutter at second city that is up and running
  104.   else if (num_cities(who) > 1) {
  105.     my_second_city = find_city_with_num(who, 2);
  106.     if (place_building_with_cost(who, "Woodcutter's Camp", my_second_city) > 0) { //try second city
  107.       wood_camp_2 = find_inactive_build(who, "Woodcutter's Camp");
  108.       wood_camp_size_2 = max_workers_at_building(who, wood_camp_2);
  109.       return wood_camp_size_2;
  110.     }
  111.     else if (place_building_with_cost(who, "Woodcutter's Camp", my_capital) > 0) { //try capital
  112.       wood_camp_2 = find_inactive_build(who, "Woodcutter's Camp");
  113.       wood_camp_size_2 = max_workers_at_building(who, wood_camp_2);
  114.       return wood_camp_size_2;
  115.     }
  116.     else if (can_pay_cost(who, "Woodcutter's Camp") > 0) return 0;
  117.   }
  118.   return -1;
  119. }
  120.  
  121. //=================================================================
  122.  
  123. //building of needed citizens
  124. int ai train_unit_with_need(int who, int high_num, String what)
  125. {
  126.  
  127.   String my_capital = find_city_with_num(who, 1);
  128.   String my_second_city = find_city_with_num(who, 2);
  129.   String my_third_city = find_city_with_num(who, 3);
  130.   int capital_id = find_city_id(my_capital);
  131.   int second_city_id = find_city_id(my_second_city);
  132.   int third_city_id = find_city_id(my_third_city);
  133.   int farms1 = 0;
  134.   int farms2 = 0;
  135.   int farms3 = 0;
  136.   int wood1 = 0;
  137.   int wood2 = 0;
  138.   int wood3 = 0;
  139.   int times = 0;
  140.   int one_camp = 0;
  141.   int two_camp = 0;
  142.   int three_camp = 0;
  143.   int aux_camp = 0;
  144.   int one_camp_max = 0;
  145.   int two_camp_max = 0;
  146.   int three_camp_max = 0;
  147.   int aux_camp_max = 0;
  148.   int one_camp_num = 0;
  149.   int two_camp_num = 0;
  150.   int three_camp_num = 0;
  151.   int aux_camp_num = 0;
  152.   int needed_workers_1 = 0;
  153.   int needed_workers_2 = 0;
  154.   int needed_workers_3 = 0;
  155.   int needed_workers_aux = 0;
  156.   int needed_wood_1 = 0;
  157.   int needed_wood_2 = 0;
  158.   int needed_wood_3 = 0;
  159.   int needed_wood_aux = 0;
  160.   int needed_farm_1 = 0;
  161.   int needed_farm_2 = 0;
  162.   int needed_farm_3 = 0;
  163.   int needed_metal_1 = 0;
  164.   int needed_metal_2 = 0;
  165.   int needed_metal_3 = 0;
  166.   int needed_metal_aux = 0;
  167.   int idle = 0;
  168.   int pop = 0;
  169.   int wc = 0;
  170.   int wc2 = 0;
  171.   int wc3 = 0;
  172.   int f = 0;
  173.   int m = 0;
  174.  
  175.   for (pop = num_type_with_queued(who, what); pop < high_num; pop++) {
  176.     //capital check
  177.     idle = find_num_idle_unit(who, what);
  178.     needed_farm_1 = 0;
  179.     farms1 = num_city_buildings(who, my_capital, "Farm", 1);
  180.     for (f = farms1; f > 0; f--) {
  181.       one_farm = find_build_at_city(who, my_capital, "Farm", 0);
  182.       one_farm_num = num_workers_at_building(who, one_farm);
  183.       if (one_farm_num == 0) {
  184.         needed_farm_1++;
  185.       }
  186.     }
  187.     needed_wood_1 = 0;
  188.     wood1 = num_city_buildings(who, my_capital, "Woodcutter's Camp", 1);
  189.     for (wc = wood1; wc > 0; wc--) {
  190.       one_camp = find_build_at_city(who, my_capital, "Woodcutter's Camp", 1);
  191.       one_camp_max = max_workers_at_building(who, one_camp);
  192.       one_camp_num = num_workers_at_building(who, one_camp);
  193.       needed_wood_1 += (one_camp_max - one_camp_num);
  194.     }
  195.     needed_metal_1 = 0;
  196.     metal1 = num_city_buildings(who, my_capital, "Mine", 1);
  197.     for (m = metal1; m > 0; m--) {
  198.       one_mine = find_build_at_city(who, my_capital, "Mine", 1);
  199.       one_mine_max = max_workers_at_building(who, one_mine);
  200.       one_mine_num = num_workers_at_building(who, one_mine);
  201.       needed_metal_1 += (one_mine_max - one_mine_num);
  202.     }
  203.     needed_workers_1 = needed_wood_1 + needed_farm_1 + needed_metal_1 - num_type_queued(who, capital_id, what) - idle;
  204.     //second city check
  205.     if (num_cities(who) > 1) {
  206.       needed_farm_2 = 0;
  207.       farms2 = num_city_buildings(who, my_second_city, "Farm", 0);
  208.       for (f = farms2; f > 0; f--) {
  209.         two_farm = find_build_at_city(who, my_second_city, "Farm", 1);
  210.         two_farm_num = num_workers_at_building(who, two_farm);
  211.         if (two_farm_num == 0) {
  212.           needed_farm_2++;
  213.         }
  214.       }
  215.       needed_wood_2 = 0;
  216.       wood2 = num_city_buildings(who, my_second_city, "Woodcutter's Camp", 1);
  217.       for (wc = wood2; wc > 0; wc--) {
  218.         two_camp = find_build_at_city(who, my_second_city, "Woodcutter's Camp", 1);
  219.         two_camp_max = max_workers_at_building(who, two_camp);
  220.         two_camp_num = num_workers_at_building(who, two_camp);
  221.         needed_wood_2 += (two_camp_max - two_camp_num);
  222.       }
  223.       needed_metal_2 = 0;
  224.       metal2 = num_city_buildings(who, my_second_city, "Mine", 1);
  225.       for (m = metal2; m > 0; m--) {
  226.         two_mine = find_build_at_city(who, my_second_city, "Mine", 1);
  227.         two_mine_max = max_workers_at_building(who, two_mine);
  228.         two_mine_num = num_workers_at_building(who, two_mine);
  229.         needed_metal_2 += (two_mine_max - two_mine_num);
  230.       }
  231.       needed_workers_2 = needed_wood_2 + needed_farm_2 + needed_metal_2 - num_type_queued(who, second_city_id, what) - idle;
  232.     }
  233.     //third city check
  234.     if (num_cities(who) > 2) {
  235.       needed_farm_3 = 0;
  236.       farms3 = num_city_buildings(who, my_third_city, "Farm", 0);
  237.       for (f = farms2; f > 0; f--) {
  238.         three_farm = find_build_at_city(who, my_third_city, "Farm", 1);
  239.         three_farm_num = num_workers_at_building(who, three_farm);
  240.         if (three_farm_num == 0) {
  241.           needed_farm_3++;
  242.         }
  243.       }
  244.       needed_wood_3 = 0;
  245.       wood3 = num_city_buildings(who, my_third_city, "Woodcutter's Camp", 1);
  246.       for (wc = wood3; wc > 0; wc--) {
  247.         three_camp = find_build_at_city(who, my_third_city, "Woodcutter's Camp", 1);
  248.         three_camp_max = max_workers_at_building(who, three_camp);
  249.         three_camp_num = num_workers_at_building(who, three_camp);
  250.         needed_wood_3 += (three_camp_max - three_camp_num);
  251.       }
  252.       needed_metal_3 = 0;
  253.       metal3 = num_city_buildings(who, my_third_city, "Mine", 1);
  254.       for (m = metal3; m > 0; m--) {
  255.         three_mine = find_build_at_city(who, my_third_city, "Mine", 1);
  256.         three_mine_max = max_workers_at_building(who, three_mine);
  257.         three_mine_num = num_workers_at_building(who, three_mine);
  258.         needed_metal_3 += (three_mine_max - three_mine_num);
  259.       }
  260.       needed_workers_3 = needed_wood_3 + needed_farm_3 + needed_metal_3 - num_type_queued(who, third_city_id, what) - idle;
  261.     }
  262.     //any wood/mine outside of cities
  263.     needed_wood_aux = 0;
  264.     for (wc = num_type(who, "Woodcutter's Camp"); wc > 0; wc--) {
  265.       aux_camp = find_build(who, "Woodcutter's Camp");
  266.       aux_camp_max = max_workers_at_building(who, aux_camp);
  267.       aux_camp_num = num_workers_at_building(who, aux_camp);
  268.       needed_wood_aux += (aux_camp_max - aux_camp_num);
  269.     }
  270.     needed_metal_aux = 0;
  271.     for (m = num_type(who, "Mine"); m > 0; m--) {
  272.       aux_mine = find_build(who, "Mine");
  273.       aux_mine_max = max_workers_at_building(who, aux_mine);
  274.       aux_mine_num = num_workers_at_building(who, aux_mine);
  275.       needed_metal_aux += (aux_mine_max - aux_mine_num);
  276.     }
  277.     needed_workers_aux = needed_wood_aux + needed_metal_aux - needed_metal_3 - needed_metal_2 - needed_metal_1 - needed_wood_3 - needed_wood_2 - needed_wood_1;
  278.     if (needed_workers_1 > 0) {
  279.       train_unit_at_with_cost(who, 1, what, capital_id);
  280.     }
  281.     else if (needed_workers_2 > 0) {
  282.       train_unit_at_with_cost(who, 1, what, second_city_id);
  283.     }
  284.     else if (needed_workers_3 > 0) {
  285.       train_unit_at_with_cost(who, 1, what, third_city_id);
  286.     }
  287.     else if (needed_workers_aux > 0) {
  288.       train_unit_with_cost(who, 1, what);
  289.     }
  290.     else {
  291.       return 1;
  292.     }
  293.   }
  294.   if (high_num <= (num_type_with_queued(who, what))) {
  295.     if (idle > 0) assign_idle(who);
  296.     return 1;
  297.   }
  298.  
  299.   else {
  300.     return -1;
  301.   }
  302.  
  303. }
  304.  
  305. //=================================================================
  306.  
  307. //idle citizen assignment
  308. int ai assign_idle (int who)
  309. {
  310.  
  311.   my_capital = find_city_with_num(who, 1);
  312.   my_second_city = find_city_with_num(who, 2);
  313.   int wood_camp = 0;
  314.   int build_new_wood_camp = 0;
  315.   int been_here = 0;
  316.  
  317.   idle = find_idle_citizen(who);
  318.   if (idle != 0)
  319.   {
  320.     for (i = num_type(who, "Woodcutter's Camp"); i > 0; i--) {
  321.       wood_camp = find_build(who, "Woodcutter's Camp");
  322.       if (num_workers_at_building(who, wood_camp) < max_workers_at_building(who, wood_camp)) {
  323.         build_new_wood_camp = 0;
  324.         return 4;
  325.       }
  326.       else {
  327.         build_new_wood_camp = 1;
  328.       }
  329.     }
  330.  
  331.     if (build_new_wood_camp == 1)
  332.     {
  333.       if (num_cities(who) >= 2) {
  334.         my_second_city = find_city_with_num(who, 2);
  335.         if(place_building_with_cost(who, "Woodcutter's Camp", my_second_city) > 0) {
  336.           wood_camp = find_inactive_build(who, "Woodcutter's Camp");
  337.           return 2;
  338.         }
  339.         else if(place_building_with_cost(who, "Woodcutter's Camp", my_capital) > 0) {
  340.           wood_camp = find_inactive_build(who, "Woodcutter's Camp");
  341.           return 1;
  342.         }
  343.         else if (can_pay_cost(who, "Woodcutter's Camp") > 0) {
  344.           wood_camp = -1;
  345.           been_here++;
  346.           if (been_here > 1) return 1;
  347.           else return 0;
  348.         }
  349.       }
  350.       else {
  351.         if(place_building_with_cost(who, "Woodcutter's Camp", my_capital) > 0) {
  352.           wood_camp = find_inactive_build(who, "Woodcutter's Camp");
  353.           return 1;
  354.         }
  355.         else if (can_pay_cost(who, "Woodcutter's Camp") > 0) {
  356.           wood_camp = -1;
  357.           return 0;
  358.         }
  359.       }
  360.     }
  361.  
  362.     if (wood_camp >= 0) {
  363.       xpos = object_position_x(who, wood_camp);
  364.       ypos = object_position_y(who, wood_camp);
  365.    
  366.       for(idle = find_idle_citizen(who); idle > -1; idle = find_idle_citizen(who)) {
  367.         unit_move_order(who, idle, xpos, ypos);
  368.       }
  369.     }
  370.   }
  371.   return -1;;
  372.  
  373. }
  374.  
  375. //woodcutter placement ...should it be done now, and build some citizens to fill it!
  376. int ai woodcutter_check(int who, int max_woodcutters, int needed_workers)
  377. {
  378.  
  379.   static int wood_check_size = 5;
  380.   static int wood_camp_placed = 0;
  381.   
  382.   if (((max_woodcutters <= wood_check_size) || num_cities(who) > 1 || find_inactive_build(who, "Small City") == 1) && wood_camp_placed == 0 && num_type(who, "Woodcutter's Camp") == 1) {
  383.     //////print_game_msg("wood camp size 1 is equal to wood check size");
  384.     wood_camp_size_2 = place_woodcutter(who);
  385.     max_woodcutters = max_woodcutters + wood_camp_size_2;
  386.     //needed_workers = max_woodcutters + num_type_with_queued(who, "Farm");   
  387.     train_unit_with_need(who, needed_workers, "Citizen");
  388.     wood_camp_placed = 1; 
  389.     return max_woodcutters;
  390.   }
  391.   else if (wood_camp_placed == 1) {
  392.     //////print_game_msg("wood camp, this is done");
  393.     return -1;
  394.   }
  395.   else {
  396.     //////print_game_msg("wood check size and wood camp size dont match");
  397.     wood_check_size++;
  398.     return 0;
  399.   }
  400.  
  401. }
  402.  
  403. //=================================================================
  404.  
  405. //dock placement
  406. int ai place_dock (int who)
  407. //returns 1 if dock is placed
  408. //returns 0 if dock can't be placed but is affordable
  409. //returns -1 if dock can't be afforded
  410. {
  411.   my_capital = find_city_with_num(who, 1); 
  412.   my_second_city = find_city_with_num(who, 2);
  413.   my_third_city = find_city_with_num(who, 3);
  414.   if (have_tech(who, "Barter")) {
  415.     if (num_cities(who) == 1) {
  416.       if (place_building_with_cost(who, "Dock", my_capital) > 0) return 1;
  417.       else {
  418.         for (wc = num_city_buildings(who, my_capital, "Woodcutter's Camp", 1); wc > 0; wc--) {
  419.           building_cycle = find_build_at_city(who, my_capital, "Woodcutter's Camp", 1);
  420.           if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  421.         }
  422.         for (f = num_city_buildings(who, my_capital, "Farm", 1); f > 0; f--) {
  423.           building_cycle = find_build_at_city(who, my_capital, "Farm", 1);
  424.           if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  425.         }
  426.         building_cycle = find_build_at_city(who, my_capital, "Library", 1);
  427.         if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  428.         if (can_pay_cost(who, "Dock") > 0) return 0;
  429.         else return -1;
  430.       }
  431.     }
  432.     else if (num_cities(who) == 2) {
  433.       if (place_building_with_cost(who, "Dock", my_capital) > 0) return 1;
  434.       else if (place_building_with_cost(who, "Dock", my_second_city) > 0) return 1;
  435.       else if (find_inactive_build(who, "Small City")) {
  436.         building_cycle = find_inactive_build(who, "Small City");
  437.         if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  438.       }
  439.       else {
  440.         for (wc = num_city_buildings(who, my_capital, "Woodcutter's Camp", 1); wc > 0; wc--) {
  441.           building_cycle = find_build_at_city(who, my_capital, "Woodcutter's Camp", 1);
  442.           if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  443.         }
  444.         for (f = num_city_buildings(who, my_capital, "Farm", 1); f > 0; f--) {
  445.           building_cycle = find_build_at_city(who, my_capital, "Farm", 1);
  446.           if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  447.         }
  448.         building_cycle = find_build_at_city(who, my_capital, "Library", 1);
  449.         if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  450.         for (wc = num_city_buildings(who, my_second_city, "Woodcutter's Camp", 1); wc > 0; wc--) {
  451.           building_cycle = find_build_at_city(who, my_second_city, "Woodcutter's Camp", 1);
  452.           if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  453.         }
  454.         for (f = num_city_buildings(who, my_second_city, "Farm", 1); f > 0; f--) {
  455.           building_cycle = find_build_at_city(who, my_second_city, "Farm", 1);
  456.           if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  457.         }
  458.         if (can_pay_cost(who, "Dock") > 0) return 0;
  459.         else return -1;
  460.       }
  461.     }
  462.     else if (num_cities(who) == 3) {
  463.       if (place_building_with_cost(who, "Dock", my_capital) > 0) return 1;
  464.       else if (place_building_with_cost(who, "Dock", my_second_city) > 0) return 1;
  465.       else if (place_building_with_cost(who, "Dock", my_third_city) > 0) return 1;
  466.       else if (find_inactive_build(who, "Small City")) {
  467.         building_cycle = find_inactive_build(who, "Small City");
  468.         if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  469.       }
  470.       else {
  471.         for (wc = num_city_buildings(who, my_capital, "Woodcutter's Camp", 1); wc > 0; wc--) {
  472.           building_cycle = find_build_at_city(who, my_capital, "Woodcutter's Camp", 1);
  473.           if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  474.         }
  475.         for (f = num_city_buildings(who, my_capital, "Farm", 1); f > 0; f--) {
  476.           building_cycle = find_build_at_city(who, my_capital, "Farm", 1);
  477.           if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  478.         }
  479.         building_cycle = find_build_at_city(who, my_capital, "Library", 1);
  480.         if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  481.         for (wc = num_city_buildings(who, my_second_city, "Woodcutter's Camp", 1); wc > 0; wc--) {
  482.           building_cycle = find_build_at_city(who, my_second_city, "Woodcutter's Camp", 1);
  483.           if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  484.         }
  485.         for (f = num_city_buildings(who, my_second_city, "Farm", 1); f > 0; f--) {
  486.           building_cycle = find_build_at_city(who, my_second_city, "Farm", 1);
  487.           if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  488.         }
  489.         for (wc = num_city_buildings(who, my_third_city, "Woodcutter's Camp", 1); wc > 0; wc--) {
  490.           building_cycle = find_build_at_city(who, my_third_city, "Woodcutter's Camp", 1);
  491.           if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  492.         }
  493.         for (f = num_city_buildings(who, my_third_city, "Farm", 1); f > 0; f--) {
  494.           building_cycle = find_build_at_city(who, my_third_city, "Farm", 1);
  495.           if (place_orphan_building_with_cost(who, "Dock", building_cycle) > 0) return 1;
  496.         }
  497.         if (can_pay_cost(who, "Dock") > 0) return 0;
  498.         else return -1;
  499.       }
  500.     }
  501.   }
  502.   return -1;
  503. }
  504.  
  505. //=================================================================
  506.  
  507. int ai place_mine(int who)
  508. {
  509.   my_capital = find_city_with_num(who, 1); 
  510.   my_second_city = find_city_with_num(who, 2);
  511.   my_third_city = find_city_with_num(who, 3);
  512.  
  513.   if (have_tech(who, "Classical Age")) {
  514.     if (num_cities(who) == 1) {
  515.       if (place_building_with_cost(who, "Mine", my_capital) > 0) return 1;
  516.       else {
  517.         for (wc = num_city_buildings(who, my_capital, "Woodcutter's Camp", 1); wc > 0; wc--) {
  518.           building_cycle = find_build_at_city(who, my_capital, "Woodcutter's Camp", 1);
  519.           if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  520.         }
  521.         for (f = num_city_buildings(who, my_capital, "Farm", 1); f > 0; f--) {
  522.           building_cycle = find_build_at_city(who, my_capital, "Farm", 1);
  523.           if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  524.         }
  525.         building_cycle = find_build_at_city(who, my_capital, "Library", 1);
  526.         if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  527.         if (can_pay_cost(who, "Mine") > 0) return 0;
  528.         else return -1;
  529.       }
  530.     }
  531.     else if (num_cities(who) == 2) {
  532.       if (place_building_with_cost(who, "Mine", my_capital) > 0) return 1;
  533.       else if (place_building_with_cost(who, "Mine", my_second_city) > 0) return 1;
  534.       else if (find_inactive_build(who, "Small City")) {
  535.         building_cycle = find_inactive_build(who, "Small City");
  536.         if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  537.       }
  538.       else {
  539.         for (wc = num_city_buildings(who, my_capital, "Woodcutter's Camp", 1); wc > 0; wc--) {
  540.           building_cycle = find_build_at_city(who, my_capital, "Woodcutter's Camp", 1);
  541.           if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  542.         }
  543.         for (f = num_city_buildings(who, my_capital, "Farm", 1); f > 0; f--) {
  544.           building_cycle = find_build_at_city(who, my_capital, "Farm", 1);
  545.           if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  546.         }
  547.         building_cycle = find_build_at_city(who, my_capital, "Library", 1);
  548.         if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  549.         for (wc = num_city_buildings(who, my_second_city, "Woodcutter's Camp", 1); wc > 0; wc--) {
  550.           building_cycle = find_build_at_city(who, my_second_city, "Woodcutter's Camp", 1);
  551.           if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  552.         }
  553.         for (f = num_city_buildings(who, my_second_city, "Farm", 1); f > 0; f--) {
  554.           building_cycle = find_build_at_city(who, my_second_city, "Farm", 1);
  555.           if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  556.         }
  557.         if (can_pay_cost(who, "Mine") > 0) return 0;
  558.         else return -1;
  559.       }
  560.     }
  561.     else if (num_cities(who) == 3) {
  562.       if (place_building_with_cost(who, "Mine", my_capital) > 0) return 1;
  563.       else if (place_building_with_cost(who, "Mine", my_second_city) > 0) return 1;
  564.       else if (place_building_with_cost(who, "Mine", my_third_city) > 0) return 1;
  565.       else if (find_inactive_build(who, "Small City")) {
  566.         building_cycle = find_inactive_build(who, "Small City");
  567.         if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  568.       }
  569.       else {
  570.         for (wc = num_city_buildings(who, my_capital, "Woodcutter's Camp", 1); wc > 0; wc--) {
  571.           building_cycle = find_build_at_city(who, my_capital, "Woodcutter's Camp", 1);
  572.           if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  573.         }
  574.         for (f = num_city_buildings(who, my_capital, "Farm", 1); f > 0; f--) {
  575.           building_cycle = find_build_at_city(who, my_capital, "Farm", 1);
  576.           if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  577.         }
  578.         building_cycle = find_build_at_city(who, my_capital, "Library", 1);
  579.         if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  580.         for (wc = num_city_buildings(who, my_second_city, "Woodcutter's Camp", 1); wc > 0; wc--) {
  581.           building_cycle = find_build_at_city(who, my_second_city, "Woodcutter's Camp", 1);
  582.           if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  583.         }
  584.         for (f = num_city_buildings(who, my_second_city, "Farm", 1); f > 0; f--) {
  585.           building_cycle = find_build_at_city(who, my_second_city, "Farm", 1);
  586.           if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  587.         }
  588.         for (wc = num_city_buildings(who, my_third_city, "Woodcutter's Camp", 1); wc > 0; wc--) {
  589.           building_cycle = find_build_at_city(who, my_third_city, "Woodcutter's Camp", 1);
  590.           if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  591.         }
  592.         for (f = num_city_buildings(who, my_third_city, "Farm", 1); f > 0; f--) {
  593.           building_cycle = find_build_at_city(who, my_third_city, "Farm", 1);
  594.           if (place_orphan_building_with_cost(who, "Mine", building_cycle) > 0) return 1;
  595.         }
  596.         if (can_pay_cost(who, "Dock") > 0) return 0;
  597.         else return -1;
  598.       }
  599.     }
  600.   }
  601.   return -1;
  602. }
  603.  
  604. //=================================================================
  605.  
  606. int ai place_farm(int who)
  607. {
  608.   int max_farms = 0;
  609.   who_nation = find_nation(who); //get player nation
  610.   my_capital = find_city_with_num(who, 1); 
  611.   my_second_city = find_city_with_num(who, 2);
  612.   my_third_city = find_city_with_num(who, 3);
  613.  
  614.   if ((who_nation == "Egyptians")&&(get_is_no_nation_powers() < 1)) max_farms = 7;
  615.   else max_farms = 5;
  616.  
  617.   if (num_cities(who) == 1) {
  618.     if (num_city_buildings(who, my_capital, "Farm", 1) < max_farms) {
  619.       if (place_building_with_cost(who, "Farm", my_capital) > 0) return 1;
  620.     }
  621.     else return 0;
  622.   }
  623.   else if (num_cities(who) == 2) {
  624.     if (num_city_buildings(who, my_capital, "Farm", 1) < max_farms) {
  625.       if (place_building_with_cost(who, "Farm", my_capital) > 0) return 1;
  626.     }
  627.     else if (num_city_buildings(who, my_second_city, "Farm", 1) < max_farms) {
  628.       if (place_building_with_cost(who, "Farm", my_second_city) > 0) return 1;
  629.     }
  630.     else return 0;
  631.   }
  632.   else if (num_cities(who) == 3) {
  633.     if (num_city_buildings(who, my_capital, "Farm", 1) < max_farms) {
  634.       if (place_building_with_cost(who, "Farm", my_capital) > 0) return 1;
  635.     }
  636.     else if (num_city_buildings(who, my_second_city, "Farm", 1) < max_farms) {
  637.       if (place_building_with_cost(who, "Farm", my_second_city) > 0) return 1;
  638.     }
  639.     else if (num_city_buildings(who, my_third_city, "Farm", 1) < max_farms) {
  640.       if (place_building_with_cost(who, "Farm", my_third_city) > 0) return 1;
  641.     }
  642.     else return 0;
  643.   }
  644.  
  645.   if (can_pay_cost(who, "Farm") > 0) return 0;
  646.  
  647.   return -1;
  648. }